package com.example.mycount2text;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static String TheJudgeList[][] =
            {       {">", ">", "<", "<", "<", ">", "<", "<"},

                    {">", ">", "<", "<", "<", ">", "<", "<"},

                    {">", ">", ">", ">", "<", ">", "<", "<"},

                    {">", ">", ">", ">", "<", ">", "<", "<"},

                    {"<", "<", "<", "<", "<", "=", "<", "<"},

                    {">", ">", ">", ">", "0", ">", ">", ">"},

                    {">", ">", ">", ">", "<", ">", "<", "<"},

                    {">", ">", ">", ">", "<", ">", "<", "<"},

            };
    HashMap<Integer,String> TheTriSave = new HashMap<>();
    private Integer savePoint=0;
    private int TheBracket=0;
    private int Symbol=0;
    Stack<String> Character = new Stack<String>();
    Stack<BigDecimal> figure = new Stack<>();
    private boolean zero_trigger = false;
    private boolean NewTrigger = false;
    private boolean NumLogic_trigger = false;
    private boolean C_trigger = false;
    private int i = 0;
    private String logic_Symbol;
    private Button one;
    private Button two;
    private Button three;
    private Button four;
    private Button five;
    private Button six;
    private Button seven;
    private Button eight;
    private Button nine;
    private Button zero;
    private Button del;
    private Button plus;
    private Button times;
    private Button minus;
    private Button cancel;
    private Button div;
    private Button equal;
    private Button point;
    private Button Remainder;
    private Button LeftBracket;
    private Button RightBracket;
    private Button Double;
    private Button sin;
    private TextView editLine;
    private Button log;
    private Button lg;
    private Button cos;
    private Button tan;
    private Button e;
    private Button bim;
    private Button ex;
    @Override
    public void onClick(View view) {
        Intent intentE = new Intent(this, MainActivityExchange.class);
        Intent intentB = new Intent(this, TheTransform.class);
        switch (view.getId()) {
            case R.id.one:
                Symbol=1;
                editLine.setText(editLine.getText() + "1");
                break;
            case R.id.two:
                Symbol=1;
                editLine.setText(editLine.getText() + "2");
                break;
            case R.id.three:
                Symbol=1;
                editLine.setText(editLine.getText() + "3");
                break;
            case R.id.four:
                Symbol=1;
                editLine.setText(editLine.getText() + "4");
                ;
                break;
            case R.id.five:
                Symbol=1;
                editLine.setText(editLine.getText() + "5");
                break;
            case R.id.six:
                Symbol=1;
                editLine.setText(editLine.getText() + "6");
                break;
            case R.id.seven:
                Symbol=1;
                editLine.setText(editLine.getText() + "7");
                break;
            case R.id.eight:
                Symbol=1;
                editLine.setText(editLine.getText() + "8");
                break;
            case R.id.nine:
                Symbol=1;
                editLine.setText(editLine.getText() + "9");
                break;
            case R.id.zero:
                Symbol=1;
                editLine.setText(editLine.getText() + "0");
                break;
            case R.id.point:
                if(Symbol!=0){
                    Symbol=0;
                editLine.setText(editLine.getText() + ".");
                }
                else Toast.makeText(this,"Input Error",Toast.LENGTH_SHORT).show();
                break;
            case R.id.cancel:
                editLine.setText("");
                FigureDestroy();
                break;
            case R.id.del:
                String a = editLine.getText().toString();
                if (a.equals("") || a.equals("0")) {
                    Toast.makeText(this, "input Error!", Toast.LENGTH_SHORT).show();
                    break;
                }
                editLine.setText(editLine.getText().toString().substring(0, editLine.getText().toString().length() - 1));
                break;
            case R.id.div:
                if(!editLine.getText().toString().equals("")&&Symbol!=0)
                editLine.setText(editLine.getText() + "/");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.times:
                if(!editLine.getText().toString().equals("")&&Symbol!=0)
                editLine.setText(editLine.getText() + "*");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.plus:
                if(!editLine.getText().toString().equals("")&&Symbol!=0)
                editLine.setText(editLine.getText() + "+");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.minus:
                if(!editLine.getText().toString().equals(""))
                editLine.setText(editLine.getText() + "-");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.Remainder:
                if(!editLine.getText().toString().equals(""))
                editLine.setText(editLine.getText() + "%");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.equal:
                if (editLine.getText().toString().equals("")) {
                    Toast.makeText(this, "input error!", Toast.LENGTH_SHORT).show();
                } else {
                    equal_cul();
                    editLine.setText(figure.peek().toString());
                    NewTrigger = true;
                    Destroy();
                }
                break;
            case R.id.RightBrackets:
                editLine.setText(editLine.getText() + ")");
                break;
            case R.id.LeftBrackets:
                editLine.setText(editLine.getText() + "(");
                break;
            case R.id.Double:
                if(!editLine.getText().toString().equals(""))
                editLine.setText(editLine.getText() + "^");
                else Toast.makeText(this,"input Error!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.sin:
                editLine.setText(editLine.getText() + "sin(");
                break;
            case R.id.cos:
                editLine.setText(editLine.getText() + "cos(");
                break;
            case R.id.tan:
                editLine.setText(editLine.getText() + "tan(");
                break;
            case R.id.log:
                editLine.setText(editLine.getText() + "ln(");
                break;
            case R.id.lg:
                editLine.setText(editLine.getText() + "lg(");
                break;
            case R.id.e:
                editLine.setText(editLine.getText() + "2.7182");
                break;
            case R.id.change:
                startActivity(intentE);
                break;
            case R.id.BIM:
                startActivity(intentB);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Allinit();
    }

    private void Allinit() {
        bim=findViewById(R.id.BIM);
        ex=findViewById(R.id.change);
        e=findViewById(R.id.e);
        lg =findViewById(R.id.lg);
        log=findViewById(R.id.log);
        tan=findViewById(R.id.tan);
        cos=findViewById(R.id.cos);
        sin=findViewById(R.id.sin);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        zero = findViewById(R.id.zero);
        point = findViewById(R.id.point);
        cancel = findViewById(R.id.cancel);
        plus = findViewById(R.id.plus);
        minus = findViewById(R.id.minus);
        times = findViewById(R.id.times);
        div = findViewById(R.id.div);
        equal = findViewById(R.id.equal);
        Double = findViewById(R.id.Double);
        del = findViewById(R.id.del);
        editLine = findViewById(R.id.editLine);
        LeftBracket = findViewById(R.id.LeftBrackets);
        RightBracket = findViewById(R.id.RightBrackets);
        Remainder = findViewById(R.id.Remainder);
        Double.setOnClickListener(this);
        LeftBracket.setOnClickListener(this);
        RightBracket.setOnClickListener(this);
        Remainder.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        div.setOnClickListener(this);
        point.setOnClickListener(this);
        zero.setOnClickListener(this);
        point.setOnClickListener(this);
        del.setOnClickListener(this);
        cancel.setOnClickListener(this);
        equal.setOnClickListener(this);
        minus.setOnClickListener(this);
        plus.setOnClickListener(this);
        times.setOnClickListener(this);
        RightBracket.setOnClickListener(this);
        LeftBracket.setOnClickListener(this);
        sin.setOnClickListener(this);
        log.setOnClickListener(this);
        lg.setOnClickListener(this);
        tan.setOnClickListener(this);
        cos.setOnClickListener(this);
        e.setOnClickListener(this);
        ex.setOnClickListener(this);
        bim.setOnClickListener(this);
    }

    private void Destroy() {
        zero_trigger = false;
        i = 0;
        C_trigger = false;
        NumLogic_trigger = false;
        Symbol=0;
    }

    private void FigureDestroy() {
        i = 0;
        Symbol=0;
        figure.clear();
        Character.clear();
        zero_trigger = false;
        C_trigger = false;
        NumLogic_trigger = false;
        NewTrigger = false;
    }

    private boolean Character_Push(String TheSymbol) {
        if (Character.empty()) {
            Character.push(TheSymbol);

        } else {
            logic_Symbol = TheLogicJudge(TheSymbol);
            if (logic_Symbol.equals("<")) {
                Character.push(TheSymbol);
            } else if (logic_Symbol.equals("=")) {
                if(savePoint!=0){
                    if(TheBracket==savePoint){
                        System.out.println("在最终运算外拦截成功!");
                        Cul_tri();
                        TheBracket--;
                        savePoint--;
                    }
                    else{
                        System.out.println("虽然在函数内,但这是正常的括号！");
                        TheBracket--;
                    }
                }
                else
                    System.out.println("现在算式内没有三角函数！！");
                Character.pop();
            } else if (logic_Symbol.equals(">")) {
                String YouNeed = editLine.getText().toString();
                System.out.println(YouNeed);
                System.out.println("现在的f是" + figure);
                System.out.println("现在的c是" + Character);
                System.out.println("现在的符号是" + TheSymbol);
                NumLogic_trigger = true;
                    System.out.println("大于时的栈顶是:" + Character.peek());
                calculate(Character.pop());
                Character.push(TheSymbol);
            }

        }
        return true;
    }

    private void equal_cul() {

        if (NewTrigger == true) {
            NewTrigger = false;
            figure.pop();
        }
        String YouNeed = editLine.getText().toString();
        System.out.println(YouNeed);
        while (i < YouNeed.length()) {
            System.out.println("要入栈的是" + YouNeed.substring(i, i + 1));
            System.out.println("栈内有f:" + figure);
            System.out.println("c栈内有:" + Character);
            int ASC2 = YouNeed.charAt(i);
            if (zero_trigger == true) {
                zero_trigger = false;
                BigDecimal Thex = figure.pop();
                String TheChangeX = Thex.toString() + "." + YouNeed.substring(i, i + 1);
                figure.push(new BigDecimal(TheChangeX));
                i++;
                continue;
            } else if (YouNeed.charAt(i) == '.') {
                zero_trigger = true;
                i++;

                continue;
            } else if (!(ASC2 > 47 && ASC2 <= 58)) {
                String helper =YouNeed.substring(i,i+1);
                if(helper.equals("l")||helper.equals("t")||helper.equals("c")||helper.equals("s")){
                    System.out.println("拦截成功！");
                    String saveString = new String();
                    while(!YouNeed.substring(i,i+1).equals("(")){
                        saveString=saveString+YouNeed.substring(i,i+1);
                        System.out.println("i为:"+i);
                        i++;
                    }
                    TheTriSave.put(savePoint,saveString);
                    savePoint++;
                    System.out.println("侦测到在途的三角函数！现在等式内有:"+(savePoint)+"个待计算的三角函数!");
                    System.out.println(TheTriSave);
                    System.out.println("拦截后的符号变成了"+YouNeed.substring(i,i+1));
                }
                if(YouNeed.substring(i,i+1).equals("(")){
                    if(savePoint==0)
                    TheBracket--;
                    System.out.println("侦测到括号！");
                    TheBracket++;
                    System.out.println("现在你有普通括号"+TheBracket+",三角函数括号"+savePoint);

                }
                if (NumLogic_trigger == true) {
                    NumLogic_trigger = false;
                    if (Character.size() != 1) {
                        String a = Character.pop();
                        if (!a.equals(Character.peek()) && (!a.equals("+") && !a.equals("-")) && (!a.equals("*") && !a.equals("/"))) {
                            System.out.println("触发了!");
                            System.out.println("此时的C" + Character);
                            Character_Push(a);
                        } else {
                            System.out.println("其二触发了！");
                            Character.push(a);
                        }
                    }
                }
                Character_Push(YouNeed.substring(i, i + 1));
                C_trigger = true;

                //Character_Push(YouNeed.substring(i));
                i++;
                continue;
            } else {
                if (C_trigger == true) {
                    figure.push(new BigDecimal(YouNeed.substring(i, i + 1)));
                    C_trigger = false;
                    i++;

                    continue;
                }
                if (figure.empty()) {
                    figure.push(new BigDecimal(YouNeed.substring(i, i + 1)));
                    i++;
                    continue;
                }
                String TheX = figure.pop().toString();
                TheX = TheX + YouNeed.substring(i, i + 1);
                System.out.println("TheX是"+TheX);
                figure.push(new BigDecimal(TheX));

                i++;
            }
        }
        while (!Character.empty()) {
            String a = Character.pop();
            if (a.equals(")")){
               if(!Character.empty())
                   if(!Character.peek().equals("(")){
                ReverseSymbol(Character.pop());
                Character_Push(a);
                continue;
                   }
            }
            System.out.println("最终循环栈内有f:" + figure);
            System.out.println("最终C栈内有:" + Character);
            ReverseSymbol(a);

        }

        return;
    }

    private void ReverseSymbol(String TheSymbol) {
        if (Character.empty()) {
            calculate(TheSymbol);
            return;
        }
        String A = Character.peek();
        if (A.equals("-") && TheSymbol.equals("+")) {
            calculate("-");
            return;
        } else if (A.equals("/") && TheSymbol.equals("*")) {
            calculate("/");
            return;

        }
        calculate(TheSymbol);
    }

    private void calculate(String TheSymbol) {
        System.out.println("计算前的Figure:" + figure);
        System.out.println("计算前的C:" + Character);
        if (!Character.empty()) {
            String X = Character.peek();
            if (X.equals("(") && TheSymbol.equals(")")) {
                if (savePoint!=0)
                {           if(TheBracket==savePoint){
                            System.out.println("在最终运算的时候该算三角函数了！");
                             Cul_tri();
                            Character.pop();
                             TheBracket--;
                            savePoint--;
                            return;
                        }
                        else{
                            System.out.println("在最终运算的时候虽然在函数内,但这是正常的括号！");
                            TheBracket--;
                        }
                    }
                    else
                        System.out.println("在最终运算的时候现在算式内没有三角函数！！");

                Character.pop();
                return;
            }
        }

        BigDecimal num2 = figure.pop();
        System.out.println("num2=" + num2);
        if(figure.empty()&&Character.empty()){
            figure.push(num2);
            return;
        }
        BigDecimal num1 = figure.pop();
        System.out.println("num1=" + num1);
        System.out.println("符号是：" + TheSymbol);
        switch (TheSymbol) {
            case "+":
                figure.push(num1.add(num2));
                System.out.println("计算后的Figure:" + figure);
                System.out.println("计算后的C:" + Character);
                break;
            case "-":
                figure.push(num1.subtract(num2));
                System.out.println("计算后的Figure:" + figure);
                System.out.println("计算后的C:" + Character);
                break;
            case "*":
                figure.push(num1.multiply(num2).setScale(6,BigDecimal.ROUND_HALF_UP));
                System.out.println("计算后的Figure:" + figure);
                System.out.println("计算后的C:" + Character);
                break;
            case "/":
                try {
                    figure.push(num1.divide(num2));
                    System.out.println("计算后的Figure:" + figure);
                    System.out.println("计算后的C:" + Character);
                } catch (Exception e) {
                    figure.push(num1.divide(num2, 6, BigDecimal.ROUND_HALF_UP));
                    System.out.println("计算后的Figure:" + figure);
                    System.out.println("计算后的C:" + Character);
                }
                break;
            case "%":
                figure.push(num1.remainder(num2));
                System.out.println("计算后的Figure:" + figure);
                System.out.println("计算后的C:" + Character);
                break;
            case "^":
                figure.push(new BigDecimal(Math.pow(num1.doubleValue(), num2.doubleValue())).setScale(6,BigDecimal.ROUND_HALF_UP));

        }

    }

    private String TheLogicJudge(String TheSymbol) {
        String thePeek = Character.peek();
        System.out.println("传入符号是:" + TheSymbol);
        System.out.println("栈顶符号:" + thePeek);
        int i = 0, j = 0;
        switch (thePeek) {
            case "+":
                i = 0;
                break;

            case "-":
                i = 1;
                break;

            case "*":
                i = 2;
                break;

            case "/":
                i = 3;
                break;

            case "(":
                i = 4;
                break;

            case ")":
                i = 5;
                break;
            case "^":
                i = 6;
                break;
            case "%":
                i = 7;
                break;


        }

        switch (TheSymbol) {

            case "+":
                j = 0;
                break;

            case "-":
                j = 1;
                break;

            case "*":
                j = 2;
                break;

            case "/":
                j = 3;
                break;

            case "(":
                j = 4;
                break;

            case ")":
                j = 5;
                break;
            case "^":
                j = 6;
                break;
            case "%":
                j = 7;
                break;

        }
        System.out.println("他们的关系是:" + TheJudgeList[i][j] + ",i是:" + i + ",J是" + j);
        return TheJudgeList[i][j];
    }
    private void Cul_tri(){
        System.out.println("TheTriSave中保存了"+TheTriSave);
        System.out.println("savePoint是"+savePoint);
       String TheX= TheTriSave.get(savePoint-1);
        System.out.println("TheX是:"+TheX);
             if(TheX.equals("sin"))
        figure.push(new BigDecimal(Math.sin(figure.pop().doubleValue())).setScale(6, BigDecimal.ROUND_HALF_UP));
        else if (TheX.equals("cos"))
            figure.push(new BigDecimal(Math.cos(figure.pop().doubleValue())).setScale(5, BigDecimal.ROUND_HALF_UP));
        else if (TheX.equals("tan"))
            figure.push(new BigDecimal(Math.tan(figure.pop().doubleValue())).setScale(5, BigDecimal.ROUND_HALF_UP));
        else if (TheX.equals("ln"))
            figure.push(new BigDecimal(Math.log(figure.pop().doubleValue())).setScale(5, BigDecimal.ROUND_HALF_UP));
        else if (TheX.equals("lg"))
            figure.push(new BigDecimal(Math.log10(figure.pop().doubleValue())).setScale(5, BigDecimal.ROUND_HALF_UP));
    }
}
